﻿using System.Collections.Generic;
using Missbot.Common;

namespace Missbot.Core.CrossingRoute
{
    public class CrossingRouteFactory
    {
        public static CrossingRouteFactory Instance { get; } = new();

        private List<IViewable> Viewables { get; }
        private List<ILearnable> Learnables { get; }
        private List<IExchangable> Exchangables { get; }

        private CrossingRouteFactory()
        {
            Viewables = AutofacSvc.LoadAllInstanceFromInterface<IViewable>();
            Learnables = AutofacSvc.LoadAllInstanceFromInterface<ILearnable>();
            Exchangables = AutofacSvc.LoadAllInstanceFromInterface<IExchangable>();
        }

        public (bool canView, string viewCommand) CheckViewable(string name)
        {
            foreach (var viewable in Viewables)
            {
                var (canView, viewCommand) = viewable.ViewableCheck(name);
                if (canView)
                {
                    return (true, viewCommand);
                }
            }

            return (false, null);
        }

        public (bool canLearn, string learnCommand) CheckLearnable(string name)
        {
            foreach (var learnable in Learnables)
            {
                var (canLearn, learnCommand) = learnable.LearnableCheck(name);
                if (canLearn)
                {
                    return (true, learnCommand);
                }
            }

            return (false, null);
        }

        public (bool canExchange, string exchangeCommand) CheckExchangable(string name)
        {
            foreach (var exchangable in Exchangables)
            {
                var (canExchage, exchangeCommand) = exchangable.ExchangableCheck(name);
                if (canExchage)
                {
                    return (true, exchangeCommand);
                }
            }

            return (false, null);
        }
    }
}